package com.ecoolex.cms.system.core.manager;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecoolex.cms.system.core.dao.VRSceneDAO;
import com.ecoolex.cms.system.entity.VRScene;
import com.ecoolex.cms.system.entity.VRScene.Column;
import com.ecoolex.cms.system.entity.VRSceneExample;
import com.ecoolex.cms.system.enums.VRSceneStatus;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.google.common.collect.Lists;

@Service
public class VRSceneManager {
	@Autowired
	private VRSceneDAO dao;
	@Autowired
	private VRSceneHotspotManager vrSceneHotspotManager;

	public Pagination<VRScene> query(Integer userId, String name, Integer[] status, Page page) {
		VRSceneExample example = new VRSceneExample();
		VRSceneExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId).andDeletedEqualTo(VRScene.NOT_DELETED);

		if (Check.notNullOrTrimEmpty(name)) {
			criteria.andSceneNameLike('%' + name + '%');
		}
		if (Check.notNullOrEmpty(status)) {
			criteria.andStatusIn(Lists.newArrayList(status));
		}

		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());

		long totalCount = dao.countByExample(example);

		List<VRScene> list = dao.selectByExample(example);

		return Pagination.create(page, totalCount, list);
	}

	public VRScene getById(Integer userId, Integer sceneId) {
		VRSceneExample example = new VRSceneExample();
		example.createCriteria().andUserIdEqualTo(userId).andSceneIdEqualTo(sceneId)
				.andDeletedEqualTo(VRScene.NOT_DELETED);

		return dao.selectOneByExample(example);
	}

	public VRScene getById(Integer sceneId) {
		VRSceneExample example = new VRSceneExample();
		example.createCriteria().andSceneIdEqualTo(sceneId).andDeletedEqualTo(VRScene.NOT_DELETED);

		return dao.selectOneByExample(example);
	}
	
	public VRScene getByOpenId(String openId) {
		VRSceneExample example = new VRSceneExample();
		example.createCriteria().andOpenIdEqualTo(openId).andDeletedEqualTo(VRScene.NOT_DELETED);

		return dao.selectOneByExample(example);
	}

	public VRScene add(VRScene record, String createUser) {
		record.setOpenId(UUID.randomUUID().toString());
		record.setStatus(VRSceneStatus.EDIT.real());
		record.setDeleted(VRScene.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));

		dao.insert(record);
		return record;
	}

	public VRScene edit(VRScene record, String updateUser) {
		VRScene old = getById(record.getUserId(), record.getSceneId());
		old.setUpdateUser(updateUser);
		old.setUpdateTime(Date.from(Instant.now()));

		BeanUtil.copy(record, old, "sceneId", "openId", "userId", "status", "deleted", "createUser", "createTime",
				"updateUser", "updateTime");

		dao.updateByPrimaryKey(old);
		return old;
	}

	@Transactional
	public Boolean delete(VRScene record, String updateUser) {
		record.setDeleted(VRScene.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted, Column.updateUser, Column.updateTime);
		vrSceneHotspotManager.deleteAllBySceneId(record.getSceneId(), updateUser);
		return true;
	}

	@Transactional
	public Boolean online(Integer userId, Integer sceneId, String updateUser) {
		VRScene scene = getById(userId, sceneId);
		if (Check.notNull(scene) && (Check.eq(scene.getStatus(), VRSceneStatus.EDIT.real())
				|| Check.eq(scene.getStatus(), VRSceneStatus.OFFLINE.real()))) {
			scene.setStatus(VRSceneStatus.ONLINE.real());
			scene.setUpdateUser(updateUser);
			scene.setUpdateTime(Date.from(Instant.now()));
			dao.updateByPrimaryKeySelective(scene, Column.status, Column.updateUser, Column.updateTime);
			return vrSceneHotspotManager.online(scene, updateUser);
		}
		return false;
	}

	@Transactional
	public Boolean offline(Integer userId, Integer sceneId, String updateUser) {
		VRScene scene = getById(userId, sceneId);
		if (Check.notNull(scene) && Check.eq(scene.getStatus(), VRSceneStatus.ONLINE.real())) {
			scene.setStatus(VRSceneStatus.OFFLINE.real());
			scene.setUpdateUser(updateUser);
			scene.setUpdateTime(Date.from(Instant.now()));
			dao.updateByPrimaryKeySelective(scene, Column.status, Column.updateUser, Column.updateTime);
			return vrSceneHotspotManager.offline(scene, updateUser);
		}
		return false;
	}

}